# Python 2.6.4
# Project Euler, Problem 89
# Copyright 2010 Talha Zaman

def dec(rom):
    R = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
    total = 0
    for i in range(len(rom)-1):
        if R[rom[i]] >= R[rom[i+1]]: total += R[rom[i]]
        else: total -= R[rom[i]]
    total += R[rom[-1]]
    return total

def rom(dec):
    one = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
    ten = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
    hun = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
    tho = ['', 'M', 'MM', 'MMM', 'MMMM']
    return tho[dec/1000]+hun[(dec/100)%10]+ten[(dec/10)%10]+one[dec%10]

numbers = [line.strip() for line in open("roman.txt").readlines()]
saved = 0
for n in numbers:
    saved += len(n) - len(rom(dec(n)))
print saved
